﻿using Admin.NET.Core.Service;
using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Microsoft.AspNetCore.Http;
namespace Admin.NET.Application;
/// <summary>
/// 路由信息服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class BoardRouteInfoService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<BoardRouteInfo> _rep;
    public BoardRouteInfoService(SqlSugarRepository<BoardRouteInfo> rep)
    {
        _rep = rep;
    }

    /// <summary>
    /// 分页查询路由信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    public async Task<SqlSugarPagedList<BoardRouteInfoOutput>> Page(BoardRouteInfoInput input)
    {
        var query = _rep.AsQueryable()
            .WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
                u.RouteName.Contains(input.SearchKey.Trim())
            )
            .WhereIF(input.RouteNo!="", u => u.RouteNo.Contains(input.RouteNo))
            .WhereIF(!string.IsNullOrWhiteSpace(input.RouteName), u => u.RouteName.Contains(input.RouteName.Trim()))
            .Select<BoardRouteInfoOutput>();
        return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加路由信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    public async Task<long> Add(AddBoardRouteInfoInput input)
    {
        var entity = input.Adapt<BoardRouteInfo>();
        await _rep.InsertAsync(entity);
        return entity.Id;
    }

    /// <summary>
    /// 删除路由信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    public async Task Delete(DeleteBoardRouteInfoInput input)
    {
        var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _rep.FakeDeleteAsync(entity);   //假删除
        //await _rep.DeleteAsync(entity);   //真删除
    }

    /// <summary>
    /// 更新路由信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    public async Task Update(UpdateBoardRouteInfoInput input)
    {
        var entity = input.Adapt<BoardRouteInfo>();
        await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取路由信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    public async Task<BoardRouteInfo> Detail([FromQuery] QueryByIdBoardRouteInfoInput input)
    {
        return await _rep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取路由信息列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    public async Task<List<BoardRouteInfoOutput>> List([FromQuery] BoardRouteInfoInput input)
    {
        return await _rep.AsQueryable().Select<BoardRouteInfoOutput>().ToListAsync();
    }

    public BoardRouteInfo getRoute(string routeNo)
    {
        return _rep.GetFirstAsync(u => u.RouteNo == routeNo).Result;
    }



}

